<?php
//信息分类控制器
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Typeinfo;
use DB;
class TypeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //$users_id = $request->session()->get('user_id');
       
       // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
      

        $list = \DB::select("select type.* from type order by concat(type.path,type.id) asc");
      
        //处理信息
        foreach($list as &$v){
            $m = substr_count($v->path,","); //获取path中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
        
       
        
        return view("admin.type.index",['list'=>$list]);
    }

    
    public  function typeinfo(Request $request)
    {
               if($request->isMethod('post')){

                }
                     
                $id   = request('id');
                
                $info = \DB::table('type')->where('id',$id)->first();
                
                return view('admin.type.type_info',['info'=>$info]);
    }
    
   
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
       // $users_id = $request->session()->get('user_id');
       
      //  $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
      
        
        $list = \DB::select("select * from type   order by concat(path,id) asc");
        
        //处理信息
        foreach($list as &$v){
            $m = substr_count($v->path,","); //获取path中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
        
       
        return view("admin.type.create",['list'=>$list]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //获取要添加的数据
      // $users_id = $request->session()->get('user_id');
       
       // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
      
        
        
        $data['name'] = request('name');
        $data['pid'] = request('pid');
        $pid = $data['pid'];
        $data['created_at'] = date('Y-m-d H:i:s');
       // $data['city_id']  = $city_id->id;
          $data['text'] = request('text');
        if(!$request->hasFile('head_img')){
                
                 $request->session()->flash('error_msg','文件不存在');
                 
                 return back();
                 
          }
            $img = $request->file('head_img');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['type_img'] = $img->store(date('Ymd'));
            
             if(!$request->hasFile('logo_img')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('logo_img');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['logo_img'] = $img->store(date('Ymd'));
            
            
            
        if($pid==0){
            $data['path']="0,";
          //   $id = \DB::table("type")->insertGetId($data);
            
        }else{
            $type = \DB::table("type")->where("id",$pid)->first();
            $data['path'] = $type->path.$pid.",";
        }
      
         //执行添加
         
      // var_dump($data);exit();
        $id = \DB::table("type")->insertGetId($data);
        //判断
        if($id>0){
            $info = "类别信息添加成功！";
        }else{
            $info = "类别信息添加失败！";
        }
        
        return redirect("type")->with("err",$info);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       
        $users = \DB::table("type")->where("id",$id)->first(); //获取要编辑的信息
      
        return view("admin.type.edit",["vo"=>$users]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
           
        $data['name'] = request('name');
        $data['updated_at'] = date('Y-m-d H:i:s');
        
         if($request->hasFile('head_img')){
                $img = $request->file('head_img');
                // 获取后缀名
                $ext = $img->extension();
                // 新文件名
                $saveName =time().rand().".".$ext;
                // 存储文件 已经不使用 move 这种方式
                // $img->move('./uploads/'.date('Ymd'),$saveName);
                // 使用 store 存储文件
                $data['type_img'] = $img->store(date('Ymd'));
             }else{
                   $info = \DB::table("type")->where("id",$id)->first();
                   $data['type_img']  = $info->type_img;
               
             }
             
             
             
             
         if($request->hasFile('logo_img')){
                $img = $request->file('logo_img');
                // 获取后缀名
                $ext = $img->extension();
                // 新文件名
                $saveName =time().rand().".".$ext;
                // 存储文件 已经不使用 move 这种方式
                // $img->move('./uploads/'.date('Ymd'),$saveName);
                // 使用 store 存储文件
                $data['logo_img'] = $img->store(date('Ymd'));
             }else{
                   $info = \DB::table("type")->where("id",$id)->first();
                   $data['logo_img']  = $info->logo_img;
               
             }
             
             
             
        $id = \DB::table("type")->where("id",$id)->update($data);
        if($id>0){
            return redirect('type');
        }else{
            return back()->with("err","修改失败!");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //先判断当前类别下是否存在子类别
        $m = \DB::table('type')->where('pid',$id)->count();
        if($m>0){
            return back()->with("err","禁止删除");
        }  
      
        \DB::table('type')->delete($id);
        return redirect("type")->with("err","删除成功！");
    }
    
    
    
    
    public function info(Request $request)
    {
         $id = request('id');
         
         if(!empty($id)){
            $typeinfo = Typeinfo::where('type_id',$id)->orderBy('is_top','desc')->get()->toArray();  
         }else{
             return back()->with("err",'不存在');
         }
         
       
        return view('admin.type.show',['list'=>$typeinfo]);
          
    }
    
    
    
    
    public function is_top(Request $request)
    {
         $id = request('id');
        
         $info = Typeinfo::find($id);

         $info->is_top  = 1;

        $status = $info->save();

         if($status){
                   return back()->with('err','推荐成功');
              }else{
                   return back()->with('err','推荐失败');
        }
      
    }
    
    public  function infoedit($id,Request $request)
    {
        $id = request('id');
        
       $info = \DB::table('type_info')->where('id',$id)->first();
        
       return view('admin.type.infoedit',['info'=>$info]);
       
    }
    
    
   
    
    
}
